﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using SecsEquip;

namespace EquipSecs300
{
    public partial class TerminalView : CMyFormEx
    {
        OnTerminalEventHandler m_teh;

        public Action PopUpWindow { get; set; }

        public TerminalView()
        {
            m_pViewName = "终端服务";

            InitializeComponent();

            m_teh += OnTerminalMsgForThread;
        }

        // 更新数据
        public override void UpdateData()
        {
            m_pSecs.m_pOnTerminal += OnTerminalMsg;
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            string TerminalInformation = textBoxSend.Text;
            RcResult rc = m_pSecs.SendSingleTerminal(TerminalInformation);
            if (rc != 0)
            {
                MessageBox.Show(rc.toString());
            }
            else
            {
                listBoxSend.Items.Add(TerminalInformation);
            }
        }

        // 响应
        public TERMINAL_REPLY OnTerminalMsg(object sender, TerminalEventArgs e)
        {
            if (this.IsHandleCreated)
            {
                BeginInvoke(m_teh, sender, e);
                return TERMINAL_REPLY.eDISPLAY_IN_WINDOW;
            }
            return TERMINAL_REPLY.eMESSAGE_WILL_NOT_BE_DISPLAY;
        }

        // 实际操作：本线程中。C#限制
        public TERMINAL_REPLY OnTerminalMsgForThread(object sender, TerminalEventArgs e)
        {
            if (e.pText != null)
            {
                textBoxRecv.Text = e.pText;
                listBoxRecv.Items.Add(e.pText);
                PopUpWindow?.Invoke();
            }
            return TERMINAL_REPLY.eDISPLAY_IN_WINDOW;
        }

        //禁用窗体的关闭按钮
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }

        private void TerminalView_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                this.TopLevel = false;
                this.FormBorderStyle = FormBorderStyle.None;
                this.Location = new Point(0, 0);
            }
        }
    }
}
